package cn.client.ui.view.main.user.emotionLog

import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import cn.client.data.remoteData.bean.PageSelectByUid
import cn.client.data.userStateManager
import cn.client.navigation.destinations.AvoidancePainCreateRoute
import cn.client.navigation.destinations.AvoidancePainPageRoute
import cn.client.navigation.destinations.EmotionLogCreateRoute
import cn.client.navigation.destinations.EmotionLogPageRoute
import cn.client.ui.components.CreateButton
import cn.client.ui.view.main.user.avoidancePain.AvoidancePainViewModel
import kotlinx.coroutines.launch


@Composable
fun EmotionLogContent(
    navController: NavController,
    viewModel: EmotionLogViewModel = hiltViewModel()
) {
    val coroutineScope = rememberCoroutineScope()
    val userIdState = userStateManager.userId.collectAsState(initial = -1)
    val userId = userIdState.value
    val pageNo: Int = 1
    val pageSize: Int = 10
    val pageSelectByUid = PageSelectByUid(uid = userId, pageNo = pageNo, pageSize = pageSize)
    val context = LocalContext.current

    val emotionLogListState = viewModel.emotionLogList.collectAsState(initial = emptyList())
    val emotionLogList = emotionLogListState.value

    coroutineScope.launch {
        viewModel.dispatch(EmotionLogViewModel.PageIntent.SelectList(pageSelectByUid))
    }

    LaunchedEffect(Unit) {
        viewModel.pageEvents.collect {
            when(it){
                is EmotionLogViewModel.PageEvent.Success -> {
                    viewModel.dispatch(EmotionLogViewModel.PageIntent.SelectList(pageSelectByUid))
                }
                is EmotionLogViewModel.PageEvent.Error -> {
                    Toast.makeText(context, "提示：${it.error}", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    LazyColumn(
        modifier = Modifier.fillMaxWidth()
    ){
        item {
            CreateButton(
                onClickCreate = {
                    // 跳转创建页面
                    navController.navigate(EmotionLogCreateRoute.route)
                }
            )
        }
        items(emotionLogList){it ->
            EmotionLogContentItem(
                text = it.incident,
                onClickSelect = {
                    // 跳转查看信息页面
                    navController.navigate(EmotionLogPageRoute.route+"/${it.id}")
                },
                onClickDelete = {
                    coroutineScope.launch {
                        viewModel.dispatch(EmotionLogViewModel.PageIntent.Delete(it.id))
                    }
                }
            )
        }

    }
}



@Composable
fun EmotionLogContentItem(
    text: String,
    onClickSelect: () -> Unit,
    onClickDelete: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(5.dp)
    ) {
        Surface(
            shape = MaterialTheme.shapes.medium,
            color = MaterialTheme.colorScheme.surfaceVariant,
            modifier = Modifier.height(80.dp)
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.fillMaxWidth()
            ) {

                Column(
                    verticalArrangement = Arrangement.Center,
                    modifier = Modifier
                        .height(80.dp)
                        .fillMaxWidth(0.8f)
                        .padding(start = 15.dp)
                ) {
                    Text(
                        text = text,
                        fontSize = 16.sp,
                        modifier = Modifier.padding(5.dp),
                        maxLines = 3,
                        overflow = TextOverflow.Ellipsis
                    )
                }
                Row(
                    horizontalArrangement = Arrangement.End,
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .height(80.dp)
                        .fillMaxWidth()
                ) {
                    Text(
                        text = "查看",
                        fontSize = 14.sp,
                        modifier = Modifier
                            .padding(5.dp)
                            .clickable { onClickSelect() })
                    Text(
                        text = "删除",
                        fontSize = 14.sp,
                        color = Color.Magenta,
                        modifier = Modifier
                            .padding(5.dp)
                            .clickable { onClickDelete() })
                }
            }
        }
    }
}